import time
import allure
from selenium.common import StaleElementReferenceException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from page.add_history_order_page import AddHistoryOrder
from page.base_page import BasePage
from page.main_page import MainPage


class HistoryOrder(BasePage):
    __MAIN_PAGE_BUTTON = By.XPATH, "//*[@role = 'menuitem']/*[text()='首页']"
    __SELF_ORDER_LIST = By.XPATH, "//*[@href='/order/selfOrder/HistoryOrder']"
    __NAME = By.XPATH, "//*[@placeholder = '请输入借贷人姓名']"
    __PHONE = By.XPATH, "//*[@placeholder = '请输入借贷人手机号']"
    __SSN = By.XPATH, "//*[@placeholder = '请输入借贷人身份证号']"
    __NUMBER = By.XPATH, "//*[@placeholder = '请输入借贷编号']"
    __CAPITAL = By.XPATH, "//*[text()='请选择资金方']"
    __PRODUCT_NAME = By.XPATH, "//*[@placeholder = '请输入产品名称']"
    __ORDER_STATUS = By.XPATH, "//*[text()='请选择订单状态']"
    __CAPITAL_PLAN = By.XPATH, "//*[text()='请选择配资方案']"
    __CREATION_START_TIME = By.XPATH, "//*[@placeholder = '开始日期']"
    __CREATION_END_TIME = By.XPATH, "//*[@placeholder = '结束日期']"
    __SEARCH_BUTTON = By.XPATH, "//*[@class = 'el-button el-button--primary el-button--default']"
    __SEARCH_FIRST_DETAILS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[2]/div/a/span"
    __DETAILS_CLOSE_BUTTON = By.XPATH, "//*[@class = 'el-icon el-drawer__close']"
    __SEARCH_FIRST_PRODUCT_TYPE = By.XPATH, "//*[@class = 'el-table__row'][1]/td[3]/div"
    __SEARCH_FIRST_NAME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[4]/div"
    __SEARCH_FIRST_ORDER_STATUS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[6]/div/span/span"
    __SEARCH_FIRST_CAPITAL = By.XPATH, "//*[@class = 'el-table__row'][1]/td[7]/div"
    __SEARCH_FIRST_PRODUCT_NAME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[8]/div"
    __VIEW_REMARKS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[16]/div/a"
    __DETAILS_PHONE = By.XPATH, "//*[text()='手机号']/following-sibling::span[1]"
    __DETAILS_SSN = By.XPATH, "//*[text()='身份证']/following-sibling::span[1]"
    __SEARCH_FIRST_CREATION_TIME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[17]/div"
    __SEARCH_QUANTITY = By.XPATH, "//*[@class = 'el-pagination__total is-first']"
    __RESET_BUTTON = By.XPATH, "//*[text() ='重置']"
    __EXPORT_BUTTON = By.XPATH, "//*[text()='导出 ']/.."
    __CUE_WORD = By.XPATH, "//*[@class = 'el-message__content']"
    __ADD_BUTTON = By.XPATH, "//*[text()='新增 ']/.."
    __REPAYMENT_BUTTON = By.XPATH, "//*[text()='还款']"
    __REPAYMENT_OK_BUTTON = By.XPATH, "//*[@aria-label = '还款']//*[text()='确 定']"

    def goto_main_page(self):
        self.find_and_click(self.__MAIN_PAGE_BUTTON)
        return MainPage(self.driver)

    def goto_self_order(self):
        with allure.step('点击订单列表'):
            self.find_and_click(self.__SELF_ORDER_LIST)
            self.wait(EC.staleness_of(self.find(By.XPATH, "//*[text()='借贷人姓名']")))
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return HistoryOrder(self.driver)

    def search_name(self, name):
        with allure.step(f"输入{name}的姓名进行查询"):
            self.find_and_send(self.__NAME, text=name)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_phone(self, phone):
        with allure.step(f"输入{phone}手机号码进行查询"):
            self.find_and_send(self.__PHONE, text=phone)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_ssn(self, ssn):
        with allure.step(f"输入{ssn}身份证号码进行查询"):
            self.find_and_send(self.__SSN, text=ssn)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_number(self, number):
        with allure.step(f"输入{number}订单编号进行查询"):
            self.find_and_send(self.__NUMBER, text=number)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_capital(self, capital):
        with allure.step(f"选择资金方{capital}"):
            self.find_and_click(self.__CAPITAL)
            self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{capital}']")
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_product_name(self, product_name):
        with allure.step(f"输入{product_name}产品名称进行查询"):
            self.find_and_send(self.__PRODUCT_NAME, text=product_name)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_capital_plan(self, capital_plan):
        with allure.step(f"选择配资方案{capital_plan}"):
            self.find_and_click(self.__CAPITAL_PLAN)
            self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{capital_plan}']")
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def search_creation_time(self, creation_start_time, creation_end_time):
        with allure.step(f"选择创建时间{creation_start_time}至{creation_end_time}"):
            self.find_and_send(self.__CREATION_START_TIME, text=creation_start_time)
            self.find_and_send(self.__CREATION_END_TIME, text=creation_end_time)
            self.find_and_send(self.__CREATION_END_TIME, text=Keys.ENTER)
            self.find_and_click(self.__SEARCH_BUTTON)
        return HistoryOrder(self.driver)

    def goto_search_results_details(self):
        with allure.step("点击当前查询结果第一位的订单详情"):
            self.find_and_click(self.__SEARCH_FIRST_DETAILS)
        return HistoryOrder(self.driver)

    def close_details(self):
        with allure.step("点击详情页关闭按钮"):
            ele = self.finds(self.__DETAILS_CLOSE_BUTTON)[0]
            ele.click()
        return HistoryOrder(self.driver)

    def get_search_results_name(self):
        with allure.step("获取当前查询结果第一位的姓名/公司名"):
            time.sleep(0.2)
            search_first_name = self.find(self.__SEARCH_FIRST_NAME).text
        return search_first_name

    def get_search_results_phone(self):
        with allure.step("获取当前订单手机号码"):
            search_first_phone = self.find(self.__DETAILS_PHONE).text
        return search_first_phone

    def get_search_results_ssn(self):
        with allure.step("获取当前订单身份证号码"):
            search_first_phone = self.find(self.__DETAILS_SSN).text
        return search_first_phone

    def get_search_results_number(self):
        with allure.step("获取当前订单编号"):
            time.sleep(0.2)
            search_first_number = self.find(self.__SEARCH_FIRST_DETAILS).text
        return search_first_number

    def get_search_results_capital(self):
        with allure.step("获取当前资金方公司"):
            time.sleep(0.2)
            search_first_capital = self.find(self.__SEARCH_FIRST_CAPITAL).text
        return search_first_capital

    def get_search_results_product_name(self):
        with allure.step("获取当前产品名称"):
            time.sleep(0.2)
            search_first_product_name = self.find(self.__SEARCH_FIRST_PRODUCT_NAME).text
        return search_first_product_name

    def get_search_results_order_status(self):
        with allure.step("获取当前订单状态"):
            while True:
                try:
                    ele = self.wait(EC.element_to_be_clickable(self.__SEARCH_FIRST_ORDER_STATUS))
                    search_first_order_status = ele.text
                    break
                except StaleElementReferenceException:
                    pass
        return search_first_order_status

    def get_search_results_product_type(self):
        with allure.step("获取当前借款人类型"):
            search_first_product_type = self.find(self.__SEARCH_FIRST_PRODUCT_TYPE).text
        return search_first_product_type

    def get_search_results_creation_time(self):
        with allure.step("获取当前创建时间"):
            search_first_creation_time = self.find(self.__SEARCH_FIRST_CREATION_TIME).text
        return search_first_creation_time

    def get_search_quantity(self):
        with allure.step("获取当前查询结果数量"):
            search_quantity = self.find(self.__SEARCH_QUANTITY).text
        return search_quantity

    def click_reset_button(self):
        with allure.step("点击重置按钮"):
            self.find_and_click(self.__RESET_BUTTON)
        return HistoryOrder(self.driver)

    def export_button(self):
        with allure.step("点击导出按钮"):
            self.find_and_click(self.__EXPORT_BUTTON)
        return HistoryOrder(self.driver)

    def get_cue_word(self):
        with allure.step("获取提示词"):
            time.sleep(1)
            cue_word = self.find(self.__CUE_WORD).text
        return cue_word

    def add_button(self):
        with allure.step("点击新增按钮"):
            self.find_and_click(self.__ADD_BUTTON)
        return AddHistoryOrder(self.driver)

    def click_repayment_button(self):
        with allure.step("点击还款按钮"):
            self.find_and_click(self.__REPAYMENT_BUTTON)
            self.find_and_click(self.__REPAYMENT_OK_BUTTON)
            time.sleep(2)
        return HistoryOrder(self.driver)

    def refresh_page(self):
        with allure.step("刷新页面"):
            self.driver.refresh()
        return HistoryOrder(self.driver)
